# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import api, fields, models, _
import time


def virtual_ocr_method(fapiao):
    print('virtual_ocr_method', fapiao)
    time.sleep(3)
    result = [{'id': one.id,
               'state': 'done',
               # todo, 其他字段的 模拟值
               } for one in fapiao]
    return result


class FapiaoOcrProvider(models.Model):
    _name = "fp.ocr.provider"
    _inherit = ['fp.operate.provider.mixin']
    _description = "Fapiao OCR Provider"
    _order = 'sequence, api_provider_id'

    def _request_methods(self):
        # to override.
        # update other  _request_methods
        return {'virtual': virtual_ocr_method}


class CompanyApiProvider(models.Model):
    _inherit = "fp.company.api.provider"

    ocr_provider_id = fields.Many2one(
        'fp.ocr.provider', string='OCR Provider')


class FapiaoFapiao(models.Model):
    _inherit = "fp.fapiao"

    def action_ocr(self):
        self2 = self.search([
            ('id', 'in', self.ids),
            ('type', '=', 'in_invoice'),
            ('state', 'in', ['confirmed', 'done']),

        ])

        return self.env['fp.fapiao.ocr'].find_by_fapiao(self2)


# haiwang 代码升级 临时用
# 放开下面的 class . 升级一次. 作用是 删除该 model 的 state 字段
# 再注释掉下面的代码, 再升级一次. 作用是 删除该 model

# class FapiaoPDF(models.Model):
#     _name = "fp.fapiao.pdf"
#     # _inherit = ['fp.fapiao.operate.mixin']
#     _description = "Fapiao pdf"

#     fapiao_id = fields.Many2one(
#         'fp.fapiao', string='Fapiao',
#         required=True, ondelete='restrict', index=True, copy=False,
#         help="被操作的发票")

#     name = fields.Char(related="fapiao_id.name")


class FapiaoOcr(models.Model):
    _name = "fp.fapiao.ocr"
    _inherit = ['fp.fapiao.operate.mixin']
    _description = "Fapiao OCR"
    # _order = 'sequence, name'

    fapiao_id = fields.Many2one(
        domain=[('type', 'in', ['in_invoice'])],
        help="被ocr及ocr结果数据")

    state = fields.Selection(
        selection_add=[
            # ('draft', 'Draft'),  # 初值
            # ('doing', 'Doing'),  # OCR 识别中
            # ('done', 'done'),  # OCR 识别完成 . 末状态  稳定状态
        ],
        string="查验控制", help="查验控制")

    ocr_provider_id = fields.Many2one(
        'fp.ocr.provider',
        string='OCR Provider',
        compute='_compute_ocr_provider_id')

    @api.depends('company_id')
    def _compute_ocr_provider_id(self):
        for one in self:
            company_api_provider = self.env[
                'fp.company.api.provider'].search_by_company(one.company_id)

            one.ocr_provider_id = company_api_provider.ocr_provider_id

    def action_request(self):
        self2 = self.search([('id', 'in', self.ids),
                             ('state', 'in', ['draft', 'done'])])

        if self2:
            for one in self2:
                one.state = 'doing'
                # one.fapiao_id.state = 'doing'

            ocr_provider_id = self2[0].ocr_provider_id
            request_method = ocr_provider_id._request_method_get()
            records = request_method(self2)

            for record in records:
                # print('action_request', record)
                one = self.browse(record['id'])
                one.write({'state': 'done'})

                values_for_fapiao = {
                    'invoice_type': record['invoice_type'],
                    'taxmachine_code': record['taxmachine_code'],
                    'type_code': record['type_code'],
                    'code': record['code'],
                    'check_code': record.get('check_code'),
                    'note': record['note'],
                    'drawer_name': record['drawer_name'],
                    'payee_name': record['payee_name'],
                    'checker_name': record['checker_name'],

                    'date_fapiao': record['date_fapiao'],

                    'payee_name': record['payee_name'],
                    'payee_name': record['payee_name'],
                    'payee_name': record['payee_name'],

                    'amount_untaxed': record['amount_untaxed'],
                    'amount_tax': record['amount_tax'],
                    'amount_total': record['amount_total'],
                    'cn_amount_total': record['cn_amount_total'],
                }

                # print(values_for_fapiao)

                one.fapiao_id.write(values_for_fapiao)
